package com.net.xpay.admin.controller.admin;

import com.net.common.model.ResultPagedList;
import com.net.common.util.LocalDateUtil;
import com.net.xpay.admin.vo.UserDailyStatVo;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.helper.RedisHelper;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.partner.PartnerManager;
import com.net.xpay.common.manager.stat.UserRealTimeStatManager;
import com.net.xpay.common.model.AggUserDailyStatModel;
import com.net.xpay.core.constant.RedisKey;
import com.net.xpay.core.model.UserDailyStatSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/admin/user-real-time-stat")
@Api(tags = "运营/后台用户经营数据实时统计", description = "运营 后台用户经营数据实时统计")
public class AdminUserRealTimeStatController {

    @Autowired
    private UserRealTimeStatManager userRealTimeStatManager;

    @Autowired
    private UserManager userManager;

    @Autowired
    private PartnerManager partnerManager;

    @Autowired
    private RedisHelper redisHelper;

    @ApiOperation("搜索商户数据")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'user-real-time-stat::search')")
    public ResultPagedList<UserDailyStatVo> aggSearch(@RequestBody UserDailyStatSearchModel searchModel) {
        if( searchModel.getType() == 0) {
            searchModel.setColumn("total_success_count");
            searchModel.setOrderby("desc");
        }
        if(searchModel.getType() == 1) {
            searchModel.setColumn("total_success_trans_amount");
            searchModel.setOrderby("desc");
        }
        if(searchModel.getType() == 2 ) {
            searchModel.setColumn("total_fail_count");
            searchModel.setOrderby("desc");
        }
        searchModel.setStartDay(LocalDateUtil.getDayStart(LocalDate.now()).toLocalDate());

        List<AggUserDailyStatModel> records = userRealTimeStatManager.aggSearch(searchModel);
        long count = userRealTimeStatManager.aggSearchCount(searchModel);
        List<UserDailyStatVo> vos = BeanCopierUtil.batchCopy(AggUserDailyStatModel.class, UserDailyStatVo.class, records);

        for (UserDailyStatVo vo : vos) {
            String failRate = null;
            if(vo.getTotalCount() != 0L){
                failRate = new BigDecimal(vo.getTotalFailCount()).multiply(new BigDecimal("100")).divide(new BigDecimal(vo.getTotalCount()), 2, RoundingMode.HALF_UP).toString();
            }
            vo.setFailRate(failRate);

            User user = userManager.getById(vo.getUserId());
            vo.setName(user.getName());
            if (user.getPartnerId() != null){
                Partner partner = partnerManager.getById(user.getPartnerId());
                vo.setPartnerName(partner.getName());
            }

        }
        ResultPagedList<UserDailyStatVo> list = new ResultPagedList<>(vos, count, searchModel);
        list.setExtra(redisHelper.get(RedisKey.USER_REAL_TIME_STAT_UPDATE_TIME));

        return list;
    }
}
